var https = require('https');
var fs = require('fs');

var options = {
    requestCert: false,
    rejectUnauthorized: false,
    passphrase : "123456",
    key: fs.readFileSync('./keys/server-key.pem'),
    ca: [fs.readFileSync('./keys/ca-cert.pem')],
    cert: fs.readFileSync('./keys/server-cert.pem')
};

var server = https.createServer(options, function (req, res) {
    res.writeHead(200);
    res.end('hello world\n');
})
server.listen(3000, '127.0.0.1');
server.on('secureConnection', function (tlsSocket) {
    
    // require("fs").writeFileSync('e:\\debug.txt', JSON.stringify(tlsSocket));
    console.log('onsecureconnection', tlsSocket.authorizationError, tlsSocket.authorized, tlsSocket.getPeerCertificate())
    if(tlsSocket.authorizationError != 'DEPTH_ZERO_SELF_SIGNED_CERT'){
        tlsSocket.end();
        
        return;
    }
    
})

server.on('newSession', function () {
    console.log('newSession')
})
